import Data.List (findIndex)

-- [1, subject, subject*subject ...]
loopValues :: Int -> [Int]
loopValues subject = iterate step $ 1
    where
        step :: Int -> Int
        step = (`mod` 20201227) . (* subject) 

unwarp :: Maybe a -> a
unwarp (Just v) = v

loopCount :: Int -> Int -> Int
loopCount subject target = 
    unwarp . findIndex (==target) . loopValues $ subject

parseInput :: String -> (Int, Int)
parseInput str = (read $ tokens !! 0, read $ tokens !! 1)
    where
        tokens = lines str

main :: IO ()
main = do
    input <- readFile "input.txt"
    let (pubKey1, pubKey2) = parseInput input
    let count1 = loopCount 7 pubKey1
    print $ (loopValues pubKey2) !! count1
